package com.ruoyi.bl.service.impl;

import com.ruoyi.bl.domain.BlUser;
import com.ruoyi.bl.domain.MemoryId;
import com.ruoyi.bl.mapper.BlUserMapper;
import com.ruoyi.bl.service.IBlUserService;
import com.ruoyi.bl.service.MemoryIdService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户Service业务层处理
 *
 * @author zhanghao
 * @date 2025-01-14
 */
@Service
public class BlUserServiceImpl implements IBlUserService {
    @Autowired
    private BlUserMapper blUserMapper;
    @Autowired
    private MemoryIdService MemoryIdService;

    /**
     * 查询用户
     *
     * @param userId 用户主键
     * @return 用户
     */
    @Override
    public BlUser selectBlUserByUserId(Long userId) {
        return blUserMapper.selectBlUserByUserId(userId);
    }

    /**
     * 查询用户列表
     *
     * @param blUser 用户
     * @return 用户
     */
    @Override
    public List<BlUser> selectBlUserList(BlUser blUser) {
        return blUserMapper.selectBlUserList(blUser);
    }

    /**
     * 新增用户
     *
     * @param blUser 用户
     * @return 结果
     */
    @Override
    public int insertBlUser(BlUser blUser) throws Exception {
        System.out.println("注册的密码为" + blUser.getPassword());//获取注册的密码,加密之后添加到数据库里
        if (blUserMapper.selectByUsername(blUser.getUserName()) != null) {
            throw new Exception("用户名已存在");
        }
        int i = blUserMapper.insertBlUser(blUser);
        //注册成功之后
        BlUser blUser1 = blUserMapper.selectByUsername(blUser.getUserName());
        Long userId = blUser1.getUserId();
        //为注册的新用户添加memoryid
        String s = MemoryIdService.CreateMemoryId(blUser);
        MemoryId memoryId = new MemoryId(userId, s);
        System.out.println("新用户的memoryid为：" + s);
        MemoryIdService.addMemoryId(memoryId);
        return i;
    }

    /**
     * 修改用户
     *
     * @param blUser 用户
     * @return 结果
     */
    @Override
    public int updateBlUser(BlUser blUser) {
        blUser.setUpdateTime(DateUtils.getNowDate());
        return blUserMapper.updateBlUser(blUser);
    }

    /**
     * 批量删除用户
     *
     * @param userIds 需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deleteBlUserByUserIds(Long[] userIds) {
        return blUserMapper.deleteBlUserByUserIds(userIds);
    }

    /**
     * 删除用户信息
     *
     * @param userId 用户主键
     * @return 结果
     */
    @Override
    public int deleteBlUserByUserId(Long userId) {
        return blUserMapper.deleteBlUserByUserId(userId);
    }

    /**
     * 根据用户名查询用户
     *
     * @param username
     * @return
     * @Author 张浩
     */
    @Override
    public BlUser selectByUserName(String username) {
        return blUserMapper.selectByUsername(username);
    }


}
